<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	protected function _initEnvironment()
	{
		
	}
	
	protected function _initLoader()
	{
		$this->bootstrap('Environment');
		$loader = Zend_Loader_Autoloader::getInstance();
		$loader->setFallbackAutoloader(true);
		$loader->suppressNotFoundWarnings(false);
		$loader->registerNamespace('Solvex_');
		$loader->registerNamespace('Zym_');
		$loader->registerNamespace('ZFDebug_');
		
		$modulesBasePath = APPLICATION_PATH . '/modules/';
		$path = $modulesBasePath . $this->getApplication()->getHost();
		try {
			$dir = new DirectoryIterator($path);
		} catch(Exception $e){
			$path = $modulesBasePath . 'fallback';
			try {
				$dir = new DirectoryIterator($path);
			} catch(Exception $ex){
				throw new Solvex_Exception("Neither the registered modules path or the default fallback is available");
			}
		}
		
		foreach($dir as $file){
			if($file->isDot() || !$file->isDir()){
				continue;
			}
			
			$module = $file->getFilename();
			
			if(Solvex_Tool_Filesystem::isSCCSDirectory($module)){
				continue;
			}
			
			new Zend_Application_Module_Autoloader(array(
				'namespace' => ucfirst($module) . '_',
				'basePath'  => $file->getPathname()
			));
		}
	}
	
	protected function _initApplication()
	{
		$this->bootstrap('Loader');
		Zend_Registry::set('app', $this->getApplication());		
		return Solvex::app();
	}
	
	protected function _initCache()
	{
		$this->bootstrap('Application');
		
		$cacheDir = ROOT_PATH . '/data/cache';
		if(!is_readable($cacheDir)){
			mkdir($cacheDir, 0777);
		} elseif(!is_writable($cacheDir)){
			chmod($cacheDir, 0777);
		}
		if (!is_writable($cacheDir)) {
        	echo "Cache directory should be writable. Run 'chmod -R 0777 path/to/var'";
            exit();
        }
		$cacheConfig = $this->getOption('system');
		if(isset($cacheConfig['cache']['options']['frontend']['lifetime'])){
			$lifetime = $cacheConfig['cache']['options']['frontend']['lifetime'];
		} else {
			$lifetime = 84600;
		}
        $backendOptions = array(
            'cache_dir'                 => $cacheDir,
            'hashed_directory_level'    => 1,
            'file_name_prefix'          => 'solvex_cache',
            'hashed_directory_umask'    => 0777
        );
        $frontendOptions = array(
			'cache_id_prefix'			=>	Solvex::app()->getHost(),
			'lifetime'					=>  $lifetime,
			'automatic_serialization'	=>  true
		);
		Zend_Registry::set('cache', Zend_Cache::factory(
        	'Core', 'File',
            $frontendOptions,
            $backendOptions,
            false,
            true
        ));
        
        return Solvex::cache();	
	}
	
	protected function _initDbAdapter()
	{
		$systemConfig = $this->getOptions('system');
		$dbConfig     = $systemConfig['database'];
		$db = Zend_Db::factory('Pdo_Mysql', array(
            'host'           => $dbConfig['host'],
            'username'       => $dbConfig['username'],
            'password'       => $dbConfig['password'],
            'dbname'         => $dbConfig['dbname'],
            'driver_options' => array(
                1002 => 'SET NAMES UTF8'
            )
        ));
        Zend_Db_Table_Abstract::setDefaultAdapter($db);
        Zend_Registry::set('db', $db);
        return Solvex::db();
	}
	
	protected function _initWebsite()
	{
		$this->bootstrap(array('Application','Cache','DbAdapter'));
	}
	
	protected function _initConfig()
	{
		$this->bootstrap(array('Application','Cache','DbAdapter','Website'));
		$config = new Solvex_Config();
		Zend_Registry::set('config', $config);
		return Solvex::config();
	}
	
	protected function _initSession()
	{
		
	}
	
	protected function _initSessionValidators()
	{
		
	}
	
	protected function _initRouter()
	{
		
	}
	
	protected function _initFrontController()
	{
		$front = Zend_Controller_Front::getInstance();
		$modulesBasePath = APPLICATION_PATH . '/modules/';
		$path = $modulesBasePath . $this->getApplication()->getHost();
		if(is_dir($path) && is_readable($path)){
			$front->addModuleDirectory($path);
		} else {
			$path = $modulesBasePath . 'fallback';
			$front->addModuleDirectory($path);
		}
		$front->setDefaultModule('core');
		$front->setParam('prefixDefaultModule',true);
		$front->setParam('displayExceptions',true);
		return $front;
	}
	
	protected function _initLocale()
	{
		$this->bootstrap('Area');
	}
	
	protected function _initView()
	{
		$view = new Solvex_View();
		$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
		$viewRenderer->setView($view);
		$viewRenderer->initView();
		return $view;	
	}
	
	protected function _initLayout()
	{
		$this->bootstrap('View');
		$layoutPath = APPLICATION_PATH . '/templates/' . Solvex::host() . '/layouts';
		Zend_Layout::startMvc(array(
			'layout' 		=> 'default',
			'layoutPath'	=> $layoutPath,
			'contentKey'	=> 'CONTENT',
			'view'			=> $this->getResource('View')
		));	
	}
	
	protected function _initFrontControllerPlugins()
	{
		$this->bootstrap('FrontController');
		$front = Zend_Controller_Front::getInstance();
		//$front->registerPlugin(new Solvex_Controller_Plugin_WebAppSetup(), 10);
		//$front->registerPlugin(new Solvex_Controller_Plugin_LanguageSetup(), 20);
		$front->registerPlugin(new Solvex_Controller_Plugin_ViewSetup(), 30);
		//$front->registerPlugin(new Solvex_Controller_Plugin_ActionSetup(), 40);
		//$front->registerPlugin(new Solvex_Controller_Plugin_PermissionSetup(), 50);	
	}
	
	protected function _initDebug()
	{
		
	}
	
}